<template>
  <div class="my-profile-wrapper">
    <el-dialog title="编辑个人资料" :visible.sync="showEditMyProfile" width="30%">
      <el-form v-model="form" label-width="100px">
        <el-form-item label="头像">
          <!-- <el-input v-model="form.avatar" placeholder="头像地址(URL)" /> -->
          <!-- <el-upload
            class="avatar-uploader"
            :action='BASE_URL'
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.avatar" :src="form.avatar" class="avatar1">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>-->
          <el-upload
            class="avatar-uploader"
            :action="BASE_URL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :headers="{'Authorization':Authorization}"
            :data="{'folderName':'avatar'}"
          >
            <img v-if="form.avatar" :src="form.avatar" class="avatar1" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nick" placeholder="昵称" />
        </el-form-item>
        <!-- <el-form-item label="性别">
          <el-radio-group v-model="form.gender">
            <el-radio :label="TIM.TYPES.GENDER_MALE">男</el-radio>
            <el-radio :label="TIM.TYPES.GENDER_FEMALE">女</el-radio>
            <el-radio :label="TIM.TYPES.GENDER_UNKNOWN">不显示</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showEditMyProfile = false">取 消</el-button>
        <el-button type="primary" @click="editMyProfile">确 定</el-button>
      </span>
    </el-dialog>
    <el-popover :width="200" trigger="click" placement="right" class="popover">
      <profile-card :profile="currentUserProfile" />
      <i class="el-icon-setting edit-my-profile" @click="handleEdit"></i>
      <avatar slot="reference" :src="currentUserProfile.avatar" class="my-avatar" />
    </el-popover>
  </div>
</template>

<script>
import { Popover, Form, FormItem, RadioGroup, Radio } from "element-ui";
import { mapState } from "vuex";
import ProfileCard from "./profile-card";
import avatar from "./avatar";
import { BASE_URL } from "@/utils/service.js";
export default {
  name: "MyProfile",
  components: {
    ElPopover: Popover,
    ProfileCard,
    ElForm: Form,
    ElFormItem: FormItem,
    ElRadioGroup: RadioGroup,
    ElRadio: Radio,
    avatar,
  },
  data() {
    return {
      showEditMyProfile: false,
      form: { avatar: "", nick: "", gender: "" },
      BASE_URL,
    };
  },
  created() {
    this.Authorization = localStorage.getItem("intrachaincode");
    this.form.nick = this.print.storeName
  },
  computed: {
    ...mapState({
      currentUserProfile: (state) => state.user.currentUserProfile,
      currentConversation: (state) => state.conversation.currentConversation,
      print: (state) => state.print.all,
    }),
    gender() {
      switch (this.currentUserProfile.gender) {
        case this.TIM.TYPES.GENDER_MALE:
          return "男";
        case this.TIM.TYPES.GENDER_FEMALE:
          return "女";
        default:
          return "暂无";
      }
    },
  },
  methods: {
    handleAvatarSuccess(res, file) {
      // console.log(res)
      this.form.avatar = res.data.url;
    },
    beforeAvatarUpload(file) {
      // 上传图片
      const typeArr = ["image/jpeg", "image/png", "image/gif"];
      const isLt2M = file.size / 1024 / 1024 < 1;
      const typeType = typeArr.indexOf(file.type);
      if (typeType < 0) {
        this.$message.error("请选择JPG/PNG/GIF格式的图片!");
      }
      if (!isLt2M) {
        this.$message.error("请选择不超过1MB的图片!");
      }
      return typeType >= 0 && isLt2M;
    },
    editMyProfile() {
      // if (this.form.avatar && this.form.avatar.indexOf('http') === -1) {
      //   this.$store.commit('showMessage', {
      //     message: '头像应该是 Url 地址',
      //     type: 'warning'
      //   })
      //   this.form.avatar = ''
      //   return
      // }
      const options = {};
      // 过滤空串
      Object.keys(this.form).forEach((key) => {
        if (this.form[key]) {
          options[key] = this.form[key];
        }
      });
      this.tim
        .updateMyProfile(options)
        .then(() => {
          this.$store.commit("showMessage", {
            message: "修改成功",
          });
          this.showEditMyProfile = false;
        })
        .catch((imError) => {
          this.$store.commit("showMessage", {
            message: imError.message,
            type: "error",
          });
        });
    },
    handleEdit() {
      const { avatar, nick } = this.currentUserProfile;
      Object.assign(this.form, { avatar, nick });
      this.showEditMyProfile = true;
    },
  },
};
</script>

<style lang="scss" scoped>
.my-profile-wrapper {
  width: 50px;
  height: 50px;
  margin: 15px;
  & > span {
    display: block;
    width: 100%;
    height: 100%;
  }
  .popover {
    padding: none;
    border: none;
    border-radius: 30px;
  }
  .my-avatar {
    cursor: pointer;
    border-radius: 50%;
  }

  .edit-my-profile {
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
  }
  /deep/ .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  /deep/ .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  /deep/ .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  /deep/ .avatar1 {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>
